const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    inputValue:'',
    videolistdata:[],
    articlelistdata:[],
    recipelistdata:[],
    malllistdata:[],
    source:"",
    isloading:true
  },
  onLoad: function (options) {
    if(options.source){
      this.setData({
        source:options.source
      })
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
    // this.search()
  },
  search(source){
    var data={
      keyword:this.data.inputValue,
      source:this.data.source
    }
    Controller.search(data).then(res=>{
      if(res.data.baseData ){
        this.setData({
          videolistdata:res.data.baseData.video,
          articlelistdata:res.data.baseData.article,
          recipelistdata:res.data.baseData.recipe,
          malllistdata:res.data.baseData.shop,
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  golink3(e){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin()
      return
    }
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url:"/pages/mall/detail?id="+id
    });
  },
  gocart(e){
    var data={
      goodsId:e.currentTarget.dataset.id,
      specs:e.currentTarget.dataset.specs,
      amount:1,
      type:1
    }
    Controller.addOrUpdate(data).then(res=>{
      wx.showToast({
        title:"加购成功"
      })
    })
   
  },
  searchSubmit(e){
    console.log(e)
    let _SearchValue = this.data.inputValue;
    let localStorageValue = [];

    if (_SearchValue != '') {

        //添加搜索历史记录
        //调用API从本地缓存中获取数据
        localStorageValue = wx.getStorageSync('searchData') || [];

        let tempSearchData = [];
        //过滤历史列表中相同的搜索记录
        for (let item of localStorageValue) {
            if (item != _SearchValue) {
                tempSearchData.push(item);
            }
        }
        //添加搜索文本进历史记录
        tempSearchData.push(_SearchValue);
        //保存至从本地缓存
        wx.setStorageSync('searchData', tempSearchData);
        
    }
    this.setData({
      videolistdata:[],
      articlelistdata:[],
      recipelistdata:[],
      malllistdata:[],
    })
    this.search()
  },
  bindInput(e){
    var value=e.detail.value || ''

      this.setData({
        inputValue: value
      })
 
  },
  golink1(e){
    var item=e.currentTarget.dataset.item;
    if(item.type==1){
      wx.navigateTo({
        url:"/pages/news/detail?id="+item.id
      });
    }else if(item.type==2 || item.type==3){
      if(item.lessonId){
        wx.navigateTo({
          url:"/pages/videoDetails/videoDetails?id="+item.id
        });
      }else{
        wx.navigateTo({
          url:"/pages/news/detail?id="+item.id
        });
      }
    }
  },
  golink2(e){
    var id=e.currentTarget.dataset.id;
    var type=e.currentTarget.dataset.type;
    wx.navigateTo({
      url:"/pages/recipesDetails/recipesDetails?id="+id
    });
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})